<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;

$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();
$basic=$this->plugin_setting('basic');
$return['banner']=$basic['image'];
$return['basic']=$basic;
set_html('title','参与下单');
if($_RQ['op']=='main'){
	$pid = intval($_RQ['pid']);
	$fileds = json_decode($this->session('fileds'),true);
	$user=pdo_get('jy_wuliu_member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	//判断是否是会员
	if($user['isvip']==1 && $user['viptime']>=time()){
		$user['ishongbao']=1;
	}else{
		$user['ishongbao']=0;
	}
	$member=pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('avatar','nickname'));
	$post=pdo_get('jy_wuliu_item',array('id'=>$pid,'uniacid'=>$_SESSION['uniacid']));
	if($post['mid'] == $_SESSION['uid']){
		//exi('不可以自己参与！','error','goback');
	}
	$post['groupbuyfloor'] = json_decode($post['groupbuyfloor'],true);
	$post['canyufileds']=json_decode($post['canyufileds'],true);
	$post['diyform']=json_decode($post['diyform'],true);
	$post['fileds']=json_decode($post['fileds'],true);
	foreach($post['fileds'] as &$pv){
		$pv['total']=1;
	}
	$address =  pdo_get('core_address',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'isdefault'=>1));
	if(empty($address)){
		$address =  pdo_get('core_address',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	}
	foreach($post['canyufileds'] as &$item){
		$item['value']=explode(';',$item['value']);
		if(!empty($address) && $item['title']=='联系人'){
			$item['data']=$address['name'];
		}
		if(!empty($address) && $item['type']==8){
			$item['data']=$address['mobile'];
		}
		if($item['type']==7){
			$address['pcd']=@json_decode($address['pcd'],true);
			$item['data']['xiangxi']=$address['address'];
			$item['data']['province']=$address['pcd']['province']?$address['pcd']['province']:'广东省';
			$item['data']['city']=$address['pcd']['city']?$address['pcd']['city']:'广州市';
			$item['data']['district']=$address['pcd']['district']?$address['pcd']['district']:'海珠区';
		}
	}
	$post['fee']=0;
	if(empty($fileds)){
		$fileds = $post['fileds'];
	}
	foreach($fileds as $fek=>&$fv){
		foreach($fv as $fk=>&$f){
			if($f['type']==18){
				$fv['total'] = $post['type']!=1?1:$fv['total'];
				$post['fee'] +=$f['data']*$fv['total'];
			}
		}
		if($post['type']==1){
			if($fv['total']==0){
					unset($fileds[$fek]);
			}
			$fv[] = array('title'=>'数量','data'=>$fv['total']);
		}
	}
	if($post['type']==1 && count($fileds)==0){
		exi('没有选择商品！','error','goback');	
	}
	if($post['type']==1){
	//阶梯拼团：
		$zhekou=$post['groupbuyfloor'][0]['zhekou']?$post['groupbuyfloor'][0]['zhekou']:9.8;
		$post['fee']=$post['fee']*$zhekou/10;
	}
	if($post['type']==0){
		$fileds=array();
	}
	$post['fileds']=$fileds;
	$post['pid']=$pid;
	$post['frompcd']=json_decode($post['frompcd'],true);
	$post['fromcity']=$post['frompcd']['city'];
	$post['topcd']=json_decode($post['topcd'],true);
	$post['tocity']=$post['topcd']['city'];
	$return['post']=$post;
	$return['province']=cfc('tools')->pcd();
	$return['member']=$member;
	$return['user']=$user;
	$return['sex']=array('1'=>'男','2'=>'女');
	exi($return);

}
if($_RQ['op']=='jieli'){
	$postdata=get_postdata();
	$pid=intval($postdata['pid']);
	$item=pdo_get('jy_wuliu_item',array('uniacid'=>$_SESSION['uniacid'],'id'=>$pid));
	if(!$pid){
		exi('没有找到该帖子，请重新选择！','error','goback');
	}
	$isjielong=pdo_get('jy_wuliu_item_jieli',array('uniacid'=>$_SESSION['uniacid'],'pid'=>$item['id'],'mid'=>$_SESSION['uid']),'id');
	if($isjielong){
		exi('你已经参与了！','error','/feelog//status=10');
	}
	if(!empty($postdata['canyufileds'])){
			if($type ==3 || $type ==0 || $type ==5 || $type ==1){
					foreach ($postdata['canyufileds'] as $cf) {
						if($cf['isopen'] && !isset($cf['data'])){
							exi('请填写：'.$cf['title'],'error','goback');
						}
					}
			}
	}
	if(!empty($postdata['diyform'])){
			foreach ($postdata['diyform'] as $df) {
				if(!isset($df['data']) && $df['ismust']){
					exi('请填写各表单内容！','error','goback');
				}
			}
	}
	$insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$_SESSION['uid'],
				'pid'=>$pid,
				'snsid'=>$item['snsid'],
				'type'=>$item['type'],
				'orderid'=>0,
				'fromuser'=>$this->session('fromuser'),
				'createtime'=>TIMESTAMP
	);
	pdo_insert('jy_wuliu_item_jieli',$insert);
	if(!empty($postdata['canyufileds']) || !empty($postdata['diyform'])){
			$data=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$_SESSION['uid'],
					'pid'=>$postdata['id'],
					'orderid'=>0,
					'type'=>$postdata['type'],
					'data'=>ijson_encode($postdata['canyufileds']),
					'diydata'=>ijson_encode($postdata['diyform']),
					'createtime'=>TIMESTAMP,
			);
			pdo_insert('jy_wuliu_item_formdata',$data);
	}
	exi('参与成功','','goback');
}
if($_RQ['op']=='post'){
		$postdata=get_postdata();
		$pid=intval($postdata['pid']);
		$type = intval($postdata['type']);
		$fileds = json_decode($this->session('fileds'),true);
		$item=pdo_get('jy_wuliu_item',array('id'=>$pid,'uniacid'=>$_SESSION['uniacid']));
		if($item['type']==6 && !$postdata['money'] && $postdata['money']<=0){
			exi('请输入拼车金额！'.$postdata['money'],'error','goback');
		}
		$item['groupbuyfloor'] = json_decode($item['groupbuyfloor'],true);
		$item['canyufileds']=json_decode($item['canyufileds'],true);
		$item['diyform']=json_decode($item['diyform'],true);
		$item['fileds']=json_decode($item['fileds'],true);
		$fee=0;
		if(empty($fileds)){
			$fileds = $item['fileds'];
		}
		foreach($fileds as &$fv){
			foreach($fv as $fk=>&$f){
				if($f['type']==18){
					$fv['total'] = $item['type']!=1?1:$fv['total'];
					$fee +=$f['data']*$fv['total'];
				}
			}
			if($item['type']==1){
				$fv[] = array('title'=>'数量','data'=>$fv['total']);
			}
		}
		
		if($item['type']==6){
			$fee=$postdata['money'];
		}
		if($item['type']==1){
			//阶梯拼团：
			$zhekou=$item['groupbuyfloor'][0]['zhekou']>0?$item['groupbuyfloor'][0]['zhekou']:8;
			$fee=$fee*$zhekou/10;
		}
		if(!empty($postdata['canyufileds'])){
			if($type ==3 || $type ==0 || $type ==5 || $type ==1){
					foreach ($postdata['canyufileds'] as $cf) {
						if($cf['isopen'] && !isset($cf['data'])){
							exi('请填写：'.$cf['title'],'error','goback');
						}
					}
			}
		}
		if(!empty($postdata['diyform'])){
			foreach ($postdata['diyform'] as $df) {
				if(!isset($df['data']) && $df['ismust']){
					exi('请填写各表单内容！','error','goback');
				}
			}
		}
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'merchid'=>$item['mid'],
			'pid'=>$postdata['id'],
			'type'=>$type,
			'fee'=>$fee,
			'ordersn'=>get_ordersn('jy_wuliu_feelog','ordersn'),
			'status'=>0,
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_wuliu_feelog',$insert);
		$id=pdo_insertid();
		if(!empty($postdata['canyufileds']) || !empty($postdata['diyform'])){
			if(!empty($postdata['canyufileds'])) {
				foreach($postdata['canyufileds'] as $v){
					if($v['title']=='联系人'){
						$name=$v['data'];
					}
					if($v['type']==8){
						$mobile=$v['data'];
					}
					if($v['type']==7){
						$xiangxi=$v['data']['xiangxi'];
						$pcd=array(
							'province'=>$v['data']['province'],
							'city'=>$v['data']['city'],
							'district'=>$v['data']['district'],
						);
					}
				}
			}
			$data=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$_SESSION['uid'],
					'pid'=>$postdata['id'],
					'orderid'=>$id,
					'type'=>$postdata['type'],
					'fileds'=>ijson_encode($fileds),
					'data'=>ijson_encode($postdata['canyufileds']),
					'diydata'=>ijson_encode($postdata['diyform']),
					'createtime'=>TIMESTAMP,
			);
			pdo_insert('jy_wuliu_item_formdata',$data);
			$address =  pdo_get('core_address',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'isdefault'=>1));
			$adddata=array(
				'name'=>$name,
				'mobile'=>$mobile,
				'address'=>$xiangxi,
				'pcd'=>ijson_encode($pcd)
			);
			if(!empty($address)){
				pdo_update('core_address',$adddata,array('id'=>$address['id'],'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
			}else{
				$adddata['mid']=$_SESSION['uid'];
				$adddata['uniacid']=$_SESSION['uniacid'];
				pdo_insert('core_address',$adddata);
				$addressid = pdo_insertid();
			}
		}
		$params=array(
			'id'=>$id,
			'title'=>'群拼车',
			'fee'=>$fee,
			//'pay_type'=>'wechat',//可选，此项作用为指定支付方式
			'no_type'=>array('hdfk'),//可选，此项作用为指定不允许使用哪种支付方式
			'back_url'=>'jy_wuliu/upjieli/payresult',//必填，指定异步支付结果通知地址，采用系统标准URL书写格式
		);
	$payid=cfc('pay')->create_pay_order($params);
	exi($payid);
}
if($_RQ['op']=='payresult'){
	$payresult=$_RQ['pay_result'];
	if($payresult['status']==1){
		$order=pdo_get('jy_wuliu_feelog',array('id'=>$payresult['id']));
		if($order['ispay'] ==1){
			exi('该订单已经支付！','error','jy_wuliu/feelog/');
		}
		if($order['fee']==$payresult['fee']){
			pdo_update('jy_wuliu_feelog',array('paytype'=>$payresult['pay_type'],'ispay'=>1,'paytime'=>time(),'status'=>1),array('id'=>$payresult['id']));
			//发信息
			//$this->notice('order_pay',$_SESSION['uid'],$store_id,$payresult['id']);
			//打印
			//$this->printmsg($text,$order['id'],1);
			//添加接力数据：
			$item=pdo_get('jy_wuliu_item',array('id'=>$order['pid'],'uniacid'=>$_SESSION['uniacid']));
			$insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$_SESSION['uid'],
				'pid'=>$item['id'],
				'snsid'=>$item['snsid'],
				'type'=>$item['type'],
				'status'=>1,
				'orderid'=>$payresult['id'],
				'createtime'=>TIMESTAMP
			);
			pdo_insert('jy_wuliu_item_jieli',$insert);
			//+余额
			model('core/member')->add_credit2($order['merchid'],$order['fee'],'拼车','jy_wuliu');
			exi('拼车成功!','success','jy_wuliu/feelog/');
		}else{
			exi('拼车失败！','error','jy_wuliu/feelog/');
		}
	}else{
		exi('支付失败。','error','jy_wuliu/feelog/');
	}
}
if($_RQ['op']=='getfileds'){
	//查询自定义字段
	$fileds=pdo_getall('jy_vote_fileds',array('uniacid'=>$_SESSION['uniacid'],'rid'=>$_RQ['id']));
	foreach($fileds as &$item){
		$item['value']=explode(';',$item['value']);
	}
	$return['post']=array();
	$return['post']['fileds']=$fileds;
	$return['success']=1;
	exi($return);
}